package qumaimai.servlet.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import qumaimai.dao.AddressDao;
import qumaimai.dao.BaseOrderDao;
import qumaimai.dao.OrderInfoDao;
import qumaimai.dao.impl.AddressDaoImpl;
import qumaimai.dao.impl.BaseOrderDaoImpl;
import qumaimai.dao.impl.OrderInfoDaoImpl;
import qumaimai.entity.Address;
import qumaimai.entity.BaseOrder;
import qumaimai.entity.vo.OrderVo;
import qumaimai.servlet.BaseServlet;

@WebServlet("/admin/AdminBaseOrderServlet")
public class AdminBaseOrderServlet   extends BaseServlet{
	BaseOrderDao dao = new BaseOrderDaoImpl();
	
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("limit"));
		String orderNo = request.getParameter("orderNo");
		List<BaseOrder> list = dao.findAll(orderNo, page, size);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", dao.getCount(orderNo));
		map.put("data", list);
		String json = JSON.toJSONString(map);
		response.getWriter().write(json);
	}
	
	public void findDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		OrderInfoDao orderInfoDao = new OrderInfoDaoImpl();
		int orderId = Integer.parseInt(request.getParameter("orderId"));
		List<OrderVo> list = orderInfoDao.findDetail(orderId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", list);
		String json = JSON.toJSONString(map);
		response.getWriter().write(json);
	}
	
	public void findAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		AddressDao addressDao = new AddressDaoImpl();
		int addressId = Integer.parseInt(request.getParameter("addressId"));
		Address address = addressDao.findById(addressId);
		String addr = address.getProvince()+address.getCity()+address.getDistrict()+address.getAddress()+" "+address.getReceiver()+" "+address.getPhone();
		response.getWriter().write(addr);
	}
	
	public void send(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		AddressDao addressDao = new AddressDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		String waybill =request.getParameter("waybill");
		int n = dao.send(id, waybill);
		response.getWriter().write("success");
	}
	
	

}